// pages/search/search.js
const app = getApp(),
    globalData = app.globalData,
    api = require('../../api/index'),
    utils = require('../../utils/util');
Page({

    /**
     * 页面的初始数据
     */
    data: {
        list: [],
        toast: {
            visible: false,
            text: ''
        },
        status: 0,
        filter: {
            keywords: "",
            brandId: "",
            categoryId: "",
            types: [1, 2, 3], // 1 人气 2 服务 3 黑标
            goodsOnState: 1,
            goodsSortByEnum: 1,
            currenetPageIndex: 1,
            pageSize: 10,
            recordCount: 0
        },
        imageBaseUri: globalData.imageBaseUri
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        this.reload();
    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {
        if (this.data.hasMore) {
            this.setData({
                'filter.currenetPageIndex': ++this.data.filter.currenetPageIndex
            });

            this.getGoodsList();
        }
    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    },

    reload() {
        this.setData({
            status: 2
        })
    },

    inputKeyword(e) {
        console.log(e, 'keyword')
        if (e.detail.value == '') {
            this.setData({
                toast: {
                    visible: true,
                    text: '请输入搜索商品名称'
                }
            })
            return
        }
        this.setData({
            'filter.keywords': e.detail.value,
            "filter.currenetPageIndex": 1,
            list: []
        });
        this.getGoodsList();
    },

    // 获取商品列表
    async getGoodsList() {
        wx.showLoading({
            title: '加载中',
        })

        try {
            let res = await api.goods.getGoodsPages(this.data.filter),
                data = res.data.ResData || {};
            console.log(res, '搜索结果列表')
            if (this.data.filter.currenetPageIndex > 1) {
                data.Items = this.data.list.concat(data.Items);
            }
            this.setData({
                list: data.Items || [],
                hasMore: data.TotalPages == 0 ? false : data.CurrentPage == data.TotalPages ? false : true,
                status: 2
            })
        } catch (e) {
            console.log(e, '获取搜索结果列表失败')
            this.setData({
                status: 1
            })
        } finally {
            wx.hideLoading()
        }

    }


})